package fit.acmetelecom;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.acmetelecom.core.BillingSystem;
import com.googlecode.refit.runner.RunnerListener;
import com.googlecode.refit.runner.TreeRunner;
import com.googlecode.refit.runner.jaxb.TestResult;

import fit.acmetelecom.fakes.FakeHtmlPrinter;

public class FitRunner {
	public static void main(String[] args) {
		RunnerListener listener = new RunnerListener() {
			Collection<TestResult> results = new ArrayList<TestResult>();

			@Override
			public void beforeTest(String testPath) {
				ApplicationContext context = new ClassPathXmlApplicationContext(
						"/spring/acme.xml");
				BeanFactory factory = context;
				AcmeTelecomFitSystem.systemUnderTest = (BillingSystem) factory
						.getBean("billsys");
				AcmeTelecomFitSystem.printer = (FakeHtmlPrinter) factory
						.getBean("htmlPrinter");
			}

			@Override
			public void afterTest(TestResult result) {
				if (!result.isPassed()) {
					results.add(result);
				}
			}

			@Override
			public void afterSuite() {
				StringBuilder builder = new StringBuilder();
				
				if( results.isEmpty() ) {
					System.out.println("All acceptance tests passed");
				} else {				
					for(TestResult result:results) {
						builder.append(result.getPath() + " failed!\n");
					}
					
					System.err.println(builder.toString());
				}
			}
		};

		TreeRunner runner = new TreeRunner(new File("fit"),
				new File("reports"), listener);
		runner.run();
	}
}
